var userCtl = require('../controller/controller.user');
var sellerCtl = require('../controller/controller.seller.js');
var industryCtl = require('../controller/controller.industry');

module.exports = {

    //保存联系人
    addContact: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        var seller = params.seller;
        var name = params.name;
        var phone = params.phone;
        var tel = params.tel;
        var des = params.des;


        var extension = params.extension;
        var industry = params.industry;


        var department = params.department;
        var position = params.position;


        //var createUid="398daa48-0dc6-4441-bead-3882d507612c";//如何获取创建人的 id
        var createUid = req.session.uid;
        var inputPararm = {
            'name': name,
            'seller': seller,
            'phone': phone,
            'tel': tel,
            'des': des,
            'createUid': createUid,
            'extension': extension,
            'industry': industry,
            'department': department,
            'position': position,
            'password': '111111',
            'enable':'0'
        };

        // contactCtl.create(inputPararm, function(err, result) {
        //
        //     if (err) {
        //         next({'isCreated': 0});
        //     }else{
        //         next({'isCreated': 1})
        //     }
        //
        // });

        userCtl.create(inputPararm, function (err, result) {

            if (err) {
                next({'isCreated': 0});
            } else {
                next({'isCreated': 1})
            }

        });

    },
    editContact: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }


        var name = params.name;
        var phone = params.phone;
        var tel = params.tel;
        var des = params.des;
        var seller = params.seller;
        var rowId = params.rowId;

        var extension = params.extension;
        var industry = params.industry;
        var department = params.department;
        var position = params.position;


        var inputPararm = {
            'name': name,
            'seller': seller,
            'phone': phone,
            'tel': tel,
            'des': des,
            'rowId': rowId,
            'extension': extension,
            'industry': industry,
            'department': department,
            'position': position,
            'password': '111111',
            'enable':'0'
        };

        // contactCtl.updateContact(inputPararm, function (err, result) {
        //     if (err) {
        //
        //         next({'isCreated': 0});
        //     } else {
        //         next({'isCreated': 1})
        //     }
        //
        // });
        userCtl.update(inputPararm, function (err, result) {
            if (err) {

                next({'isCreated': 0});
            } else {
                next({'isCreated': 1})
            }

        });



    },
    //删除联系人信息
    delContact: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var rowId = params.rowId;
        userCtl.softDelContact({'rowId': rowId}, function (err, result) {
            if (err) {
                next({'isDel': 0});
            } else {
                next({'isDel': 1});
            }

        })

    },
    //获取联系人信息
    getContactInfo: function (req, res, next) {
        contactCtl.getInfo(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            sellerCtl.getSellerList(params, function (err, resultseller) {
                if (err) {
                    res.redirect('/error');
                    return;
                }
                next({
                    'contact': result.obj,
                    'sellerlist': resultseller.obj
                });
            })

        })

    },


    //获取联系人详情
    getContactDetail: function (req, res, next) {
        var params = req.query;
        var action = params.action;
        if (action == 'add') {

            sellerCtl.getSellerList(params, function (err, resultseller) {
                if (err) {
                    res.redirect('/error');
                    return;
                }
                industryCtl.getIndustryList(null, function (err, resultindustry) {
                    if (err) {
                        handleError(res, err);
                    }
                    next({
                        'contact': null,
                        'action': 'add',
                        'sellerlist': resultseller.obj,
                        'industrylist': resultindustry.obj
                    });
                })
            });

        } else if (action == 'detail') {
            //showdetail
            userCtl.getInfo(params, function (err, result) {
                if (err) {
                    res.redirect('/error');
                    return;
                }
                sellerCtl.getSellerList(params, function (err, resultseller) {
                    if (err) {
                        res.redirect('/error');
                        return;
                    }
                    industryCtl.getIndustryList(null, function (err, resultindustry) {
                        if (err) {
                            handleError(res, err);
                        }
                        next({
                            'contact': result.obj,
                            'action': 'detail',
                            'sellerlist': resultseller.obj,
                            'industrylist': resultindustry.obj
                        });
                    })

                })

            });
        }

    },

    //获取通讯录列表
    getContactList: function (req, res, next) {

        var params = req.query;
        if (params.queryStr == null) {
            params.queryStr = "";
        }

        var role = req.session.userinfo.role;

        if (role == '4' || role == '5') {

            params.rowId = req.session.userinfo.seller.rowId;

            sellerCtl.getSellerContactList(params, function (err, result) {
                if (err) {

                    res.redirect('/error');
                    return;
                }
                next({'list': result.obj});
            });
        } else {
            sellerCtl.getSellerContactList(params, function (err, result) {
                if (err) {

                    res.redirect('/error');
                    return;
                }
                next({'list': result.obj});
            });
        }

    },
    //获取卖方列表
    getSellerList: function (req, res, next) {
        var params = req.query;
        sellerCtl.getEnabledSellerList(params, function (err, result) {
            if (err) {

                res.redirect('/error');
                return;
            }
            next({'list': result.obj});
        })
    },

    //保存卖方信息
    saveSeller: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        var name = params.seller;
        var des = params.des;

        //var createUid="398daa48-0dc6-4441-bead-3882d507612c";//如何获取创建人的 id
        var createUid = req.session.uid;
        var inputPararm = {'name': name, 'createUid': createUid, 'enable': '1'};

        sellerCtl.create(inputPararm, function (err, result) {
            if (err) {

                next({'isCreated': 0});
            } else {
                next({'isCreated': 1})
            }
            ;
        })


    },
    //删除卖方信息
    delSeller: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var rowId = params.rowId;
        sellerCtl.softDelSeller({'rowId': rowId}, function (err, result) {
            if (err) {
                next({'isDel': 0});
            } else {
                next({'isDel': 1});
            }

        })

    }

};
